package com.xh.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DTable;
import com.xh.web.service.IDColumnService;
import com.xh.web.service.IDTableService;
import com.xh.web.util.StringUtils;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 表Controller
 *
 * @author dongxuefei
 * @date 2020-09-04
 */
@RestController
@RequestMapping("/db/table")
public class DTableController extends BaseController
{
    @Autowired
    private IDTableService dTableService;
    @Autowired
    private IDColumnService dColumnService;




    /**
     * 查询首页表列表,按条件模糊查询
     */
    @GetMapping("/selectLike")
    public TableDataInfo selectTable(DTable dTable) {
        List<DTable> list = dTableService.selectDTableList(dTable);
        return getDataTable(list);
    }

    /**
     * 获取树结构
     * nxh
     */
    @GetMapping("/tree")
    public AjaxResult selectTableTree(DTable dTable) {
        return AjaxResult.success(dTableService.getTableTree(dTable.getProjectId(),dTable.getContentId()));
    }

    @GetMapping(value = "/get")
    public AjaxResult getInfo(String id)
    {
        return AjaxResult.success(dTableService.selectDTableById(id));
    }

    /**
     * 新增表
     */
    @PostMapping("/insertTable")
    public AjaxResult add(@RequestBody DTable dTable)
    {
        DTable dTable1=dTableService.selectDTableByCode(dTable.getTableCode(),dTable.getProjectId());
            if (dTable1 !=null){
                return AjaxResult.error("表代码已存在");
            }

        return toAjax(dTableService.insertDTable(dTable));
    }

    /**
     * 修改表
     */
    @PutMapping("/updateTable")
    public AjaxResult edit(@RequestBody DTable dTable)
    {
       DTable dTable1= dTableService.selectDTableById(dTable.getId());
       if(StringUtils.isNotNull(dTable.getTableCode())){
           DTable table=dTableService.selectDTableByCode(dTable.getTableCode(),dTable1.getProjectId());
           if (!dTable.getTableCode().equals(dTable1.getTableCode())){
               if (table!=null){
                   return AjaxResult.error("表代码已存在");

               }
           }
       }
        return toAjax(dTableService.updateDTable(dTable));
    }

    /**
     * 删除表
     */
    @DeleteMapping("delete/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(dTableService.deleteDTableById(id));
    }

    /**
     * 预览sql
     */
    @GetMapping("/preview/{tableId}")
    public AjaxResult preview(@PathVariable("tableId") String tableId) throws IOException
    {
        Map<String, String> dataMap = dTableService.previewCode(tableId,null);
        return AjaxResult.success(dataMap);
    }

    /**
     * 获取表之间的关系
     */
    @GetMapping("/relation")
    public TableDataInfo relation(DTable dTable) {
        List<DTable> list = dTableService.getRelation(dTable);
        return getDataTable(list);
    }

}
